package com.lagou.client;

import com.lagou.model.Question;
import com.lagou.model.QuestionMessage;
import com.lagou.model.User;
import com.lagou.model.UserMessage;
import com.lagou.server.ServerDao;

import java.io.IOException;
import java.util.Scanner;

/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;

    /**
     * 通过构造方法实现成员变量的初始化
     * @param cic
     */
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws Exception {
        while(true) {
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            //Scanner sc = new Scanner(System.in);
            //int choose = sc.nextInt();
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    clientStudentLogin();
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现管理主界面的绘制
     * @throws InterruptedException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientStudent() throws Exception {
        while(true) {
            System.out.println("\n\n\t    欢迎进入学员系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 考试管理");
            System.out.println("     [2] 查分管理");
            System.out.print("   [3] 修改密码");
            System.out.println("     [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进入的管理系统：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: exam(); break;
                case 2: ; break;
                case 3: updatePassword(); break;
                case 0:
                    System.out.println("正在退出");
                    Thread.sleep(5000);
                    clientMainPage();
                    return;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }

    private void exam() {

    }

    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    public void clientManagerLogin() throws Exception {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            clientManage();

        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    private void updatePassword() throws Exception {
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage userMessage = new UserMessage("updatePassword", new User(userName,password));
        cic.getOos().writeObject(userMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        userMessage = (UserMessage) cic.getOis().readObject();
        if("success".equals(userMessage.getType())) {
            System.out.println(userMessage.getUser().toString());
            System.out.println("修改学员密码成功！");
        } else {
            System.out.println("修改学员密码失败！");
        }
    }

    /**
     * 自定义成员方法实现学员登录的功能
     */
    public void clientStudentLogin() throws Exception {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入学员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("studentCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，");
            clientStudent();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    /**
     * 自定义成员方法实现管理主界面的绘制
     * @throws InterruptedException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientManage() throws Exception {
        while(true) {
            System.out.println("\n\n\t    欢迎进入管理员系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 学员管理");
            System.out.println("     [2] 考题管理");
            System.out.println("   [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进入的管理系统：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: clientUser(); break;
                case 2: clientQuestion(); break;
                case 0: System.out.println("正在退出管理系统...");
                    Thread.sleep(5000);
                    return;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 考题系统
     * @throws Exception
     */
    public void clientQuestion() throws Exception{

        while(true) {
            System.out.println("\n\n\t    欢迎进入考题系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 增加考题");
            System.out.println("     [2] 删除考题");
            System.out.print("   [3] 修改考题");
            System.out.println("     [4] 查找考题");
            System.out.println("   [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进行的操作：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: addQuestion(); break;
                case 2: deleteQuestion() ; break;
                case 3: updateQuestion(); break;
                case 4: queryQuestion(); break;
                case 0: quitQuestion();
                    Thread.sleep(5000);
                    return;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }

    private void quitQuestion() throws Exception {
        QuestionMessage questionMessage = new QuestionMessage("quitQuestion", new Question());
        cic.getOos().writeObject(questionMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        questionMessage = (QuestionMessage) cic.getOis().readObject();
        if("success".equals(questionMessage.getType())) {
            System.out.println("查询学员账户成功！");
        } else {
            System.out.println("查询学员账户失败！");
        }
        clientMainPage();
    }

    /**
     * 查询考题
     */
    private void queryQuestion() throws IOException, ClassNotFoundException {

        System.out.println("请输入学员账户信息：");
        String title = ClientScanner.getScanner().next();
        QuestionMessage questionMessage = new QuestionMessage("queryQuestion", new Question(title));
        cic.getOos().writeObject(questionMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        questionMessage = (QuestionMessage) cic.getOis().readObject();
        if("success".equals(questionMessage.getType())) {
            System.out.println(questionMessage.getQuestion().toString());
            System.out.println("查询考题成功！");
        } else {
            System.out.println("查询考题失败！");
        }
    }

    /**
     * 修改
     */
    private void updateQuestion() {
    }

    private void deleteQuestion() {
    }

    /**
     * 添加
     */
    private void addQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入考题题目：");
        String title = ClientScanner.getScanner().next();
        System.out.println("请输入选项A：");
        String keyA = ClientScanner.getScanner().next();
        System.out.println("请输入选项B：");
        String keyB = ClientScanner.getScanner().next();
        System.out.println("请输入选项C：");
        String keyC = ClientScanner.getScanner().next();
        System.out.println("请输入选项D：");
        String keyD = ClientScanner.getScanner().next();
        QuestionMessage questionMessage = new QuestionMessage("addQuestion", new Question(title, keyA,keyB,keyC,keyD));
        cic.getOos().writeObject(questionMessage);
        System.out.println("客户端发送增加的账户信息成功！");
        // 接收服务器发来的处理结果并给出提示
        questionMessage = (QuestionMessage) cic.getOis().readObject();
        if("success".equals(questionMessage.getType())) {
            System.out.println("增加学员账户成功！");
        } else {
            System.out.println("增加学员账户失败！");
        }
    }

    /**|
     * 学员系统
     * @throws Exception
     */
    public void clientUser() throws Exception{
        while(true) {
            System.out.println("\n\n\t    欢迎进入学员系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 增加学员");
            System.out.println("     [2] 删除学员");
            System.out.print("   [3] 修改学员");
            System.out.println("     [4] 查找学员");
            System.out.println("   [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进行的操作：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: addUser(); break;
                case 2: deleteUser(); break;
                case 3: updateUser(); break;
                case 4: queryUser(); break;
                case 0: quit();
                    Thread.sleep(5000);
                    break;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }

    private void quit() throws Exception {
        UserMessage userMessage = new UserMessage("quit", new User());
        cic.getOos().writeObject(userMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        userMessage = (UserMessage) cic.getOis().readObject();
        if("success".equals(userMessage.getType())) {
            System.out.println("退出成功！");
        } else {
            System.out.println("退出失败！");
        }
        clientMainPage();
    }

    /**
     * 查询
     */
    private void queryUser()throws Exception {
        System.out.println("请输入学员账户信息：");
        String userName = ClientScanner.getScanner().next();
        UserMessage userMessage = new UserMessage("queryUser", new User(userName));
        cic.getOos().writeObject(userMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        userMessage = (UserMessage) cic.getOis().readObject();
        if("success".equals(userMessage.getType())) {
            System.out.println(userMessage.getUser().toString());
            System.out.println("查询学员账户成功！");
        } else {
            System.out.println("查询学员账户失败！");
        }
    }

    /**
     * 修改
     */
    private void updateUser() throws IOException, ClassNotFoundException {
        System.out.println("请输入学员账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入学员密码：");
        String password = ClientScanner.getScanner().next();
        UserMessage userMessage = new UserMessage("updateUser", new User(userName,password));
        cic.getOos().writeObject(userMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        userMessage = (UserMessage) cic.getOis().readObject();
        if("success".equals(userMessage.getType())) {
            System.out.println(userMessage.getUser().toString());
            System.out.println("修改学员账户成功！");
        } else {
            System.out.println("修改学员账户失败！");
        }
    }

    /**
     * 删除
     */
    private void deleteUser() throws IOException, ClassNotFoundException {
        System.out.println("请输入学员账户信息：");
        String userName = ClientScanner.getScanner().next();
        UserMessage userMessage = new UserMessage("deleteUser", new User(userName));
        cic.getOos().writeObject(userMessage);
        System.out.println("客户端发送账号信息成功！");
        // 接收服务器发来的处理结果并给出提示
        userMessage = (UserMessage) cic.getOis().readObject();
        if("success".equals(userMessage.getType())) {
            //System.out.println(userMessage.getUser().toString());
            System.out.println("删除学员账户成功！");
        } else {
            System.out.println("删除学员账户失败！");
        }
    }

    /**
     * 增加学员
     */
    public void addUser() throws Exception{
        System.out.println("请输入学员账户信息：");
        String userName = ClientScanner.getScanner().next();
        String password = "111111";
        UserMessage userMessage = new UserMessage("userCheck", new User(userName, password));
        cic.getOos().writeObject(userMessage);
        System.out.println("客户端发送增加的账户信息成功！");
        // 接收服务器发来的处理结果并给出提示
        userMessage = (UserMessage) cic.getOis().readObject();
        if("success".equals(userMessage.getType())) {
            System.out.println(userMessage.getUser().toString());
            System.out.println("增加学员账户成功！");
        } else {
            System.out.println("增加学员账户失败！");
        }

    }
}
